package cn.sypt.yuhao.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.mail.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.sypt.yuhao.dao.ManagerBO;
import cn.sypt.yuhao.domain.Manager;
import cn.sypt.yuhao.domain.Video;
import cn.sypt.yuhao.model.Tool;

public class ManagerServlet extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	
	
		
		ManagerBO mb=new ManagerBO();
		
		String flag=request.getParameter("flag");
		
		String msg="";
		
		if(flag.equals("managerlogin"))
		{
			String mname=request.getParameter("mname");
			String mpw=request.getParameter("mpw");
			
			if(mb.checkManager(mname, mpw))
			{
				Manager man=new Manager();
				man=mb.getManager(mname);
				request.getSession().setAttribute("man", man);
				ArrayList al=new ArrayList();
				al=mb.getUsers();
				request.setAttribute("users", al);
				request.getRequestDispatcher("/admin/index.jsp").forward(request, response);
			}else
			{
				msg="用户名或密码错误!";
				request.setAttribute("msg",msg);
				request.getRequestDispatcher("/admin/login.jsp").forward(request, response);
			}
		}
		else if(flag.equals("logout"))
		{
			
			msg="注销成功!";
			request.setAttribute("msg", msg);
			request.getSession(true).removeValue("man");;
			request.getRequestDispatcher("/admin/login.jsp").forward(request, response);
		}
		else if(flag.equals("usergl"))
		{
			String flag1=request.getParameter("flag1");
			String uid=request.getParameter("uid");
			if(flag1!=null&&uid!=null)
			{
				if(mb.userDj(flag1, uid))
				{
					msg="操作成功!";
					request.setAttribute("msg",msg);
					ArrayList al=new ArrayList();
					al=mb.getUsers();
					request.setAttribute("users", al);
					
					request.getRequestDispatcher("/admin/index.jsp").forward(request, response);
					response.getWriter().print("<script language=javascript>self.location=document.referrer;</script>");
					return;
				}
			}
			ArrayList al=new ArrayList();
			al=mb.getUsers();
			request.setAttribute("users", al);
			request.getRequestDispatcher("/admin/index.jsp").forward(request, response);
		}
		else if(flag.equals("plgl"))
		{
			String flag1=request.getParameter("flag1");
			String cid=request.getParameter("cid");
			if(flag1!=null)
			{
				if(flag1.equals("dl"))
				{
				mb.dlComment(cid);
				mb.dlPlAllReply(cid);
				System.out.println("pinlun guanli");
				response.getWriter().print("<script language=javascript>self.location=document.referrer;</script>");
				return;
				}
			}
			ArrayList al=new ArrayList();
			al=mb.getComments();
			request.setAttribute("comments", al);
			request.getRequestDispatcher("/admin/comment.jsp").forward(request, response);
		}
		else if(flag.equals("hfgl"))
		{			
			String cid=request.getParameter("cid");
			String flag1=request.getParameter("flag1");
			String rid=request.getParameter("rid");
			HttpSession session=request.getSession(true);
			if(cid!=null)
			{
			//放入session
			session.setAttribute("cid",cid);
			}
			//得到session中的fid
			String cid1=(String) session.getAttribute("cid");
			if(flag1!=null)
			{
				if(flag1.equals("dl"))
				{
				mb.Dlreply(rid);
				response.getWriter().print("<script language=javascript>self.location=document.referrer;</script>");
				return ;
				}
			}
			ArrayList al=mb.getReplys(cid1);
			request.setAttribute("replys", al);
			request.getRequestDispatcher("/admin/reply.jsp").forward(request, response);
		}
		else if(flag.equals("sygl"))
		{
			String flag1=request.getParameter("flag1");
			String spid=request.getParameter("spid");
			
			if(flag1!=null&&spid!=null)
			{
				mb.videoShdl(flag1, spid);
				System.out.println("删除lllll");
				System.out.println(flag1+"/////"+spid);
				response.getWriter().print("<script language=javascript>self.location=document.referrer;</script>");
				return;
			}
			ArrayList al=mb.getVideos();
			request.setAttribute("videos", al);
			request.getRequestDispatcher("/admin/sygl.jsp").forward(request, response);
		}
		else if(flag.equals("sycategl"))
		{
			String flag1=request.getParameter("flag1");
			String fid=request.getParameter("fid");
			String fname=request.getParameter("fname");
			String fnum=request.getParameter("fnum");
			
			if(flag1!=null)
			{
				mb.tdxCategory(flag1, fid, fname,fnum);
				if(!flag1.equals("xg"))
				{
					response.getWriter().print("<script language=javascript>self.location=document.referrer;</script>");
					return;
				}
			}
			ArrayList al=mb.getSycate();
			request.setAttribute("sycate", al);
			request.getRequestDispatcher("/admin/sycategl.jsp").forward(request, response);
		}
		else if(flag.equals("syacategl"))
		{
			//得到cate的fid
			String fid=request.getParameter("fid");
			HttpSession session=request.getSession(true);
			if(fid!=null)
			{
			//放入session
			session.setAttribute("fid",fid);
			}
			//得到session中的fid
			String fid1=(String) session.getAttribute("fid");
			String flag1=request.getParameter("flag1");
			String aid=request.getParameter("aid");
			String aname=request.getParameter("aname");
			if(flag1!=null)
			{
				mb.tdxACategory(flag1, aid, aname,fid1);
				if(!flag1.equals("xg"))
				{
					response.getWriter().print("<script language=javascript>self.location=document.referrer;</script>");
					return;
				}
			}			
	        ArrayList al=mb.getSyacate(fid1);
			request.setAttribute("syacate", al);
			request.getRequestDispatcher("/admin/syacategl.jsp").forward(request, response);
		}
		else if(flag.equals("sybcategl"))
		{
			//得到bcate的aid
			String aid=request.getParameter("aid");
			HttpSession session=request.getSession(true);
			if(aid!=null)
			{
			//放入session
			session.setAttribute("aid",aid);
			}
			//得到session中的fid
			String aid1=(String) session.getAttribute("aid");
			String flag1=request.getParameter("flag1");
			String bid=request.getParameter("bid");
			String bname=request.getParameter("bname");
			if(flag1!=null)
			{
				mb.tdxBCategory(flag1, bid, bname,aid1);
				
				if(!flag1.equals("xg"))
				{
					response.getWriter().print("<script language=javascript>self.location=document.referrer;</script>");
					return;
				}
			}			
	        ArrayList al=mb.getSybcate(aid1);
			request.setAttribute("sybcate", al);
			request.getRequestDispatcher("/admin/sybcategl.jsp").forward(request, response);
		}
		else if(flag.equals("mgchgl"))
		{
			//得到bcate的aid
			String wid=request.getParameter("wid");
			String flag1=request.getParameter("flag1");
			String flag2=request.getParameter("flag2");
			String words=request.getParameter("words");
			if(flag1!=null)
			{
				mb.tdxMgch(flag1, wid, words);
				
				if(!flag1.equals("xg"))
				{
					response.getWriter().print("<script language=javascript>self.location=document.referrer;</script>");
					return;
				}
				
			}	
			if(flag2!=null)
			{
				if(flag2.equals("xg"))
				{
					request.setAttribute("words", words);
					words=new Tool().getString(words);
					System.out.println(words);
					request.getRequestDispatcher("/admin/mgchxg.jsp").forward(request, response);
					return;
				}
			}
	        ArrayList al=mb.getMgch();
			request.setAttribute("mgch", al);
			request.getRequestDispatcher("/admin/mgchgl.jsp").forward(request, response);
		}
		else if(flag.equals("examgl1"))
		{
			ArrayList al=mb.getVideos();
			request.setAttribute("videos", al);
			
			request.getRequestDispatcher("/admin/examgl1.jsp").forward(request, response);
		}
		else if(flag.equals("examgl2"))
		{
			String spid=request.getParameter("spid");
			if(spid!=null)
			{							
			request.getSession().setAttribute("spid", spid);
			}
	        spid=(String) request.getSession().getAttribute("spid");
			String flag1=request.getParameter("flag1");
			String tid=request.getParameter("tid");
			String ttm=request.getParameter("ttm");
			String ta=request.getParameter("ta");
			String tb=request.getParameter("tb");
			String tc=request.getParameter("tc");
			String td=request.getParameter("td");
			String tda=request.getParameter("tda");
			String note=request.getParameter("note");
			if(flag1!=null)
			{
				mb.tdxExam(flag1, spid,tid, ttm, ta, tb, tc, td, tda, note);
			}
			ArrayList al=mb.getExamBySpid(spid);
			Video video=mb.getVideo(spid);
			System.out.println(video.getSpname());
			request.setAttribute("exam", al);
			request.setAttribute("video", video);
			request.getRequestDispatcher("/admin/examgl2.jsp").forward(request, response);
		}

	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);
	}



}
